package com.todo.strategy.pipeline.infrastructure.repository.impl;

import com.todo.strategy.pipeline.common.enums.ExecuteStatusEnum;
import com.todo.strategy.pipeline.common.enums.StrategyActionEnum;
import com.todo.strategy.pipeline.infrastructure.entity.po.PipelineRecordPO;
import com.todo.strategy.pipeline.infrastructure.mapper.PipelineRecordMapper;
import com.todo.strategy.pipeline.infrastructure.repository.PipelineRecordRepository;
import org.springframework.stereotype.Repository;


import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author azhebuxing
 * @date 2025/2/28 20:55
 * @description
 */
@Repository
public class PipelineRecordRepositoryImpl implements PipelineRecordRepository {

    @Resource
    private PipelineRecordMapper pipelineRecordMapper;

    @Override
    public PipelineRecordPO queryPipelineRecordByUniKey(String uniKey, StrategyActionEnum actionType) {
        if (uniKey == null || actionType == null) {
            return null;
        }
        PipelineRecordPO pipelineRecordPO = pipelineRecordMapper.queryPipelineRecordByUniKey(uniKey, actionType.getType());
        return pipelineRecordPO;
    }

    @Override
    public boolean insertPipelineRecord(PipelineRecordPO pipelineRecordPO) {
        if (pipelineRecordPO == null) {
            return false;
        }
        boolean inserted = pipelineRecordMapper.insertPipelineRecord(pipelineRecordPO);
        return inserted;
    }

    @Override
    public boolean updatePipelineRecord(PipelineRecordPO pipelineRecordPO) {
        if (pipelineRecordPO == null || pipelineRecordPO.getId() == null) {
            return false;
        }
        boolean inserted = pipelineRecordMapper.updatePipelineRecord(pipelineRecordPO);
        return inserted;
    }

    @Override
    public List<PipelineRecordPO> queryPipelineRecordByTimeRange(StrategyActionEnum actionType, ExecuteStatusEnum status, Date startTime, Date endTime) {
        if (actionType == null || status == null || startTime == null || endTime == null) {
            return Collections.emptyList();
        }
        if (startTime.after(endTime) || startTime.after(new Date())) {
            return Collections.emptyList();
        }
        return pipelineRecordMapper.queryPipelineRecordByTimeRange(actionType.getType(), status.getType(), startTime, endTime);
    }
}
